﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataStructure
{
    /// <summary>
    /// 线性表
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class LinkList<T> : IEnumerator<T>
    {
        #region 字段
        private T[] _element;//存储线性表的数组
        private int _pos;//线性表当前下标的位置
        private int _size;//线性表的最大长度
        private int _maxSize;//线性表的最大长度
        #endregion

        #region 属性
        /// <summary>
        /// 存储线性表的数组
        /// </summary>
        public T[] Element
        {
            get { return _element; }
            set { _element = value; }
        }
        /// <summary>
        /// 线性表当前下标的位置
        /// </summary>
        public int Pos
        {
            get { return _pos; }
        }
        /// <summary>
        /// 线性表的最大长度
        /// </summary>
        public int Size
        {
            get { return _size; }
        }
        /// <summary>
        /// 线性表的最大长度
        /// </summary>
        public int MaxSize
        {
            get { return _maxSize; }
        }
        #endregion

        #region  构造函数
        public LinkList(int size)
        {
            _pos = -1;
            _size = 0;
            _maxSize = size;
            _element = new T[_maxSize];
        }
        public LinkList()
            : this(10)
        { }

        #endregion

        #region 工具方法
        #region 插入
        /// <summary>
        /// 插入一个元素
        /// </summary>
        /// <param name="pos">下标位置</param>
        /// <param name="data">插入的数据</param>
        /// <returns></returns>
        public bool insert(int pos, T data)
        {
            if (pos < 0 || pos > _maxSize - 1)
            {
                return false;
            }
            else
            {
                for (int i = pos; i < _maxSize - 1; i++)
                {
                    _element[i + 1] = _element[i];
                }
                _pos++;
                _size++;
                _element[pos] = data;
                return true;
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除一个元素
        /// </summary>
        /// <param name="pos">删除元素的位置</param>
        /// <returns></returns>
        public bool delete(int pos)
        {
            if (pos < 0 || pos > _pos)
            {
                return false;
            }
            else
            {
                for (int i = pos; i <= _pos - 1; i++)
                {
                    _element[i] = _element[i + 1];
                }
                _pos--;
                _size--;
                return true;
            }
        }
        #endregion

        #region 追加
        /// <summary>
        /// 追加一个元素
        /// </summary>
        /// <param name="data">要追加的元素</param>
        /// <returns></returns>
        public bool append(T data)
        {
            if (_pos < _maxSize - 1)
            {
                _pos++;
                _size++;
                _element[_pos] = data;
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 判断是否为空表
        /// <summary>
        /// 判断是否为空表
        /// </summary>
        /// <returns></returns>
        public bool isEmpty()
        {
            return _size == 0;
        }
        #endregion

        #region 判断线性表是否已满
        /// <summary>
        /// 判断线性表是否已满
        /// </summary>
        /// <returns></returns>
        public bool isFull()
        {
            return _size == _maxSize;
        }
        #endregion

        #region 获得线性表中的元素
        /// <summary>
        /// 获得线性表中的元素
        /// </summary>
        /// <param name="pos">元素下标</param>
        /// <returns></returns>
        public T getElement(int pos)
        {
            if (pos < 0 || pos > _pos)
            {
                throw new IndexOutOfRangeException();
            }
            return _element[pos];
        }
        #endregion

        #region 清空线性表
        public void Clear()
        {
            _pos = -1;
            _size = 0;
        }
        #endregion
        #endregion

        #region IEnumerator<T> 成员

        public T Current
        {
            get 
            {
                try
                {
                    return _element[_pos];
                }
                catch (IndexOutOfRangeException)
                {

                    throw new IndexOutOfRangeException();
                }
            }
        }

        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IEnumerator 成员

        object System.Collections.IEnumerator.Current
        {
            get { throw new NotImplementedException(); }
        }

        public bool MoveNext()
        {
            _pos++;
            return _pos < _size;
        }

        public void Reset()
        {
            _pos = -1;
        }

        #endregion
    }
}
